import axios from 'axios'
import { baseUrl } from './HttpRequestUrl'
import router from "@/router";

export const instance = axios.create({
  baseURL: baseUrl.toString(),
  timeout: 100000,
  withCredentials: true,
});

instance.interceptors.request.use(function (config) {
  // 在发起请求请做一些业务处理
  return config;
}, function (error) {
  // 对请求失败做处理
  return Promise.reject(error);
});
instance.interceptors.response.use( // 回复拦截，主要针对部分回掉数据状态码进行处理
  response => {

    //拦截响应，做统一处理
      switch (response.data.statusCode) {

        case 2001:
          // @ts-ignore
          window.location.href = data.data
          break
        case 3006:
          router.replace({
            path: 'Login',
            query: {
              redirect: router.currentRoute.fullPath
            }
          })
          break
        default:
          if ( !response.data.status ) {
            alert(response.data.message)
          }

      }

    return response
  },
  error => {
    return Promise.reject(error)
  }
)
